//
//  NSString+Time.m
//  XiaoMiao
//
//  Created by 23 on 2017/11/30.
//  Copyright © 2017年 smallyou. All rights reserved.
//

#import "NSString+Time.h"

@implementation NSString (Time)

//服务器的window时间戳字符串转换成格式化的时间字符串
- (NSString *)app_formatTimeString:(NSString *)dateFormat
{
    NSTimeInterval timeInter = [self doubleValue];      //1511357319000 服务器传递过来的
    NSLog(@"llq777: %@--%f", self, timeInter);
    timeInter = timeInter / 1000.0;                     //1512032425.108096 本地linux时间戳
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInter];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = dateFormat;
    return [formatter stringFromDate:date];
}

/**服务器的window时间戳字符串转换成NSDate*/
- (NSDate *)app_dateFromServer
{
    NSTimeInterval timeInter = [self doubleValue];      //1511357319000 服务器传递过来的
    timeInter = timeInter / 1000.0;                     //1512032425.108096 本地linux时间戳
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInter];
    return date;
}
 
+ (NSString *)app_timeFormatType:(TimeFormatType)timeFormatType date:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *components = [calendar components:unit fromDate:date];
    
    // 当前时间的年、月、日、时、分、秒
    NSInteger year  = components.year;
    NSInteger month = components.month;
    NSInteger day   = components.day;
    NSInteger hour  = components.hour;
    NSInteger min   = components.minute;
    NSInteger second = components.second;
    
    NSString *timeStr = nil;
    switch (timeFormatType) {
        case TimeFormatTypeYYYYMMDDHHmmSS:
            timeStr = [NSString stringWithFormat:@"%ld-%02ld-%02ld %02ld:%02ld:%02ld", year, month, day, hour, min, second];
            break;
            
        case TimeFormatTypeYYYYMMDD:
            timeStr = [NSString stringWithFormat:@"%ld-%02ld-%02ld", year, month, day];
            break;
            
        case TimeFormatTypeHHmmSS:
            timeStr = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hour, min, second];
            break;
            
        case TimeFormatTypeMMDD:
            timeStr = [NSString stringWithFormat:@"%02ld-%02ld", month, day];
            break;
            
        case TimeFormatTypeHHmm:
            timeStr = [NSString stringWithFormat:@"%02ld:%02ld", hour, min];
            break;
            
        case TimeFormatTypeWeek: {
            NSArray * arrWeek = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
            timeStr = [NSString stringWithFormat:@"%@",[arrWeek objectAtIndex:components.weekday - 1]];
        }
            break;
    }
    
    return timeStr;
}
@end


@implementation NSString (Distance)

//服务器的千米单位的数值，转换成可显示的距离
- (NSString *)app_displayDistance
{
    NSString *string = @"距离:";
    CGFloat distance = [self floatValue];    //单位 千米
    if (distance < 1) {
        distance = distance * 1000;     // 单位 米
        string = [NSString stringWithFormat:@"距离:%0.0f米", distance];
    }else{
        string = [NSString stringWithFormat:@"距离:%0.0f千米", distance];
    }
    
    return string;
}


@end
